<template>
  <!-- 创建组合产品 -->

  <div class="wh100 addbox flexbox flexbox_column">
    <div class="flexbox">
      <div class="flexCenter">
        <el-button type="success" size="default" style="width: 100px;height: 36px;" @click="returnHandler"
          >返回</el-button
        >
      </div>

      <div class="wh100 flexbox flex_center">
        <h2 style="font-size:20px;">{{ infoModel.isAdd == 'add' ? '新增组合产品' : `${infoModel.sku} --- 修改` }}</h2>
      </div>
    </div>

    <div class="infoBox flexbox_column mtop10">
      <el-form :model="infoModel" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
        <div class="flex1 flexbox_row">
          <el-card class="leftbox">
            <!-- <div class="flex1 leftbox"> -->
            <h3 class="flexbox f20">产品信息</h3>
            <el-row class="flexbox">
              <el-col :span="24" class="leftboxs">
                <el-col :span="24">
                  <el-form-item label="产品SKU：" prop="sku">
                    <el-input
                      v-model.trim="infoModel.sku"
                      placeholder="请输入"
                      maxlength="100"
                      show-word-limit
                      clearable
                    ></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="产品名称：" prop="name">
                    <el-input
                      v-model.trim="infoModel.name"
                      placeholder="请输入"
                      maxlength="100"
                      show-word-limit
                      clearable
                    ></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="产品名称en：" prop="en_name">
                    <el-input
                      v-model.trim="infoModel.en_name"
                      placeholder="请输入"
                      maxlength="100"
                      show-word-limit
                      clearable
                    ></el-input>
                  </el-form-item>
                </el-col>
              </el-col>

              <div class="photo">
                <!-- 图片 -->
                <upload-imgs
                  ref="photoEle"
                  :rules="photoRules"
                  :multiple="true"
                  :maxNum="1"
                  :value="infoModel.picture_url"
                  :autoUpload="false"
                />
              </div>
            </el-row>

            <el-row>
              <el-col :span="5">
                <el-form-item label="长：" prop="length">
                  <div class="flexbox_row">
                    <el-input-number
                      v-model="infoModel.length"
                      :min="1"
                      :max="999999"
                      style="min-width:150px;max-width:200px;"
                      label="请输入"
                    ></el-input-number>
                  </div>
                </el-form-item>
              </el-col>

              <el-col :span="5">
                <el-form-item label="宽：" prop="width">
                  <div class="flexbox_row">
                    <el-input-number
                      v-model="infoModel.width"
                      :min="1"
                      :max="999999"
                      style="min-width:150px;max-width:200px;"
                      label="请输入"
                    ></el-input-number>
                  </div>
                </el-form-item>
              </el-col>
              <el-col :span="7">
                <el-form-item label="高：" prop="height">
                  <!-- lwh_type：1:inch 2:cm -->
                  <div class="flexbox_row">
                    <el-input-number
                      v-model="infoModel.height"
                      :min="1"
                      :max="999999"
                      style="min-width:150px;max-width:200px;"
                      label="请输入"
                    ></el-input-number>
                    <el-select v-model="infoModel.lwh_type" style="min-width:70px;max-width:100px;" placeholder="unit">
                      <el-option label="inch" :value="1"></el-option>
                      <el-option label="cm" :value="2"></el-option>
                    </el-select>
                  </div>
                </el-form-item>
              </el-col>

              <el-col :span="7">
                <el-form-item label="重量：" prop="weight">
                  <!-- weight_type： 1:lb/oz 2:kg -->
                  <div class="flexbox_row">
                    <el-input-number
                      v-model="infoModel.weight"
                      :min="1"
                      :max="999999"
                      style="min-width:150px;max-width:200px;"
                      label="请输入"
                    ></el-input-number>

                    <el-select
                      v-model="infoModel.weight_type"
                      style="min-width:70px;max-width:100px;"
                      placeholder="unit"
                    >
                      <el-option label="lb/oz" :value="1"></el-option>
                      <el-option label="kg" :value="2"></el-option>
                    </el-select>
                  </div>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="6" class="flexbox">
                <el-form-item label="货物类型：" prop="goods_type">
                  <el-select v-model="infoModel.goods_type" clearable placeholder="请选择">
                    <el-option label="包裹" :value="1"></el-option>
                    <el-option label="信封" :value="2"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="6" class="flexbox">
                <el-form-item label="含电池：" prop="is_battery">
                  <el-select v-model="infoModel.is_battery" clearable placeholder="请选择">
                    <el-option label="没有" value="0"></el-option>
                    <el-option label="有" value="1"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="6" class="flexbox">
                <el-form-item label="是否带有液体：" prop="is_liquid">
                  <el-select v-model="infoModel.is_liquid" clearable placeholder="请选择">
                    <el-option label="没有" value="0"></el-option>
                    <el-option label="有" value="1"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="6" class="flexbox">
                <el-form-item label="是否带磁：" prop="is_magnetized">
                  <el-select v-model="infoModel.is_magnetized" clearable placeholder="请选择">
                    <el-option label="没有" value="0"></el-option>
                    <el-option label="有" value="1"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <h3 class="flexbox mtop30 f20">产品组成</h3>
              <el-col :span="24" class="flexbox">
                <el-form-item label="SKU:" prop="portfolio_products">
                  <template v-for="(item, i) in infoModel.portfolio_products">
                    <div :key="i" :class="i > 0 ? 'mtop10' : ''" class="flexbox_column">
                      <div class="flexbox_row">
                        <div class="flexbox_column inlineBlock">
                          <el-select v-model="item.son_id" clearable class="w300px" placeholder="请选择">
                            <el-option
                              v-for="(item, i) in toExamineProductList"
                              :key="i"
                              :label="item.name"
                              :value="item.id"
                            ></el-option>
                          </el-select>
                        </div>

                        <div class="mleft20 flexbox_column inlineBlock">
                          <span>数量：</span>

                          <el-input-number
                            v-model="item.son_num"
                            :min="1"
                            :max="100"
                            label="输入数量"
                          ></el-input-number>

                          <el-button type="warning" circle v-if="i > 0" @click="removeItem(i, 'portfolio_products')">
                            <i class="el-icon-delete" style="font-size:18px;"></i>
                          </el-button>
                        </div>
                      </div>
                    </div>
                  </template>

                  <div
                    class="inlineBlock mtop10"
                    :class="infoModel.portfolio_products.length > 1 ? 'mr50' : ''"
                    v-if="infoModel.portfolio_products.length < toExamineProductList.length"
                    @click="addHandler('portfolio_products')"
                  >
                    <el-button type="success" circle>
                      <i class="el-icon-circle-plus" style="font-size:18px;"></i>
                    </el-button>
                  </div>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <h3 class="flexbox mtop30 f20">产品关联信息</h3>
              <el-col :span="24" class="flexbox">
                <el-form-item label="条码信息：" prop="product_barcodes">
                  <template v-for="(item, i) in infoModel.product_barcodes">
                    <div :key="i" class="flexbox_column">
                      <div class="flexbox_row">
                        <div class="flexbox_column inlineBlock">
                          <div class="flexbox">条码名称</div>
                          <el-input
                            v-model="item.custom_code_name"
                            class="w300px"
                            maxlength="100"
                            show-word-limit
                            placeholder="请输入"
                            clearable
                          ></el-input>
                        </div>

                        <div class="mleft20 flexbox_column inlineBlock">
                          <div class="flexbox">条形码</div>
                          <el-input
                            v-model="item.custom_code"
                            placeholder="请输入"
                            class="w300px"
                            maxlength="100"
                            show-word-limit
                            clearable
                          ></el-input>

                          <el-button type="warning" circle v-if="i > 0" @click="removeItem(i, 'custom_code')">
                            <i class="el-icon-delete" style="font-size:18px;"></i>
                          </el-button>
                        </div>
                      </div>
                    </div>
                  </template>

                  <div
                    class="inlineBlock mtop10"
                    :class="infoModel.product_barcodes.length > 1 ? 'mr50' : ''"
                    @click="addHandler('custom_code')"
                  >
                    <el-button type="success" circle>
                      <i class="el-icon-circle-plus" style="font-size:18px;"></i>
                    </el-button>
                  </div>
                </el-form-item>
              </el-col>

              <el-col :span="24" class="flexbox">
                <el-form-item label="平台映射：" prop="shops">
                  <template v-for="(item, i) in infoModel.shops">
                    <div :key="i" class="flexbox_column">
                      <div class="flexbox_row">
                        <div class="flexbox_column inlineBlock">
                          <div class="flexbox">平台</div>

                          <el-input
                            v-model="item.shop_sku"
                            class="w300px"
                            maxlength="100"
                            show-word-limit
                            placeholder="请输入"
                            clearable
                          ></el-input>
                        </div>

                        <div class="flexbox_column inlineBlock mleft20">
                          <div class="flexbox">映射条形码</div>
                          <el-input
                            v-model="item.shop_code"
                            class="w300px"
                            maxlength="100"
                            show-word-limit
                            placeholder="请输入"
                            clearable
                          ></el-input>

                          <el-button type="warning" circle v-if="i > 0" @click="removeItem(i, 'shop_code')">
                            <i class="el-icon-delete" style="font-size:18px;"></i>
                          </el-button>
                        </div>
                      </div>
                    </div>
                  </template>

                  <div
                    class="inlineBlock mtop10"
                    :class="infoModel.shops.length > 1 ? 'mr50' : ''"
                    @click="addHandler('shop_code')"
                  >
                    <el-button type="success" circle>
                      <i class="el-icon-circle-plus" style="font-size:18px;"></i>
                    </el-button>
                  </div>
                </el-form-item>
              </el-col>
            </el-row>
          </el-card>

          <el-card class="mleft10 rightbox">
            <h3 class="flexbox f20">产品附加信息</h3>

            <el-row>
              <el-col :span="24">
                <el-form-item label="产品品类：" prop="category_id">
                  <el-cascader
                    :options="categoryList"
                    style="width: 100%;"
                    filterable
                    v-model="infoModel.category_id"
                    clearable
                  ></el-cascader>
                </el-form-item>
              </el-col>

              <el-col :span="24">
                <el-form-item label="海关编码：">
                  <el-input
                    v-model.trim="infoModel.customs_code"
                    placeholder="请输入"
                    show-word-limit
                    maxlength="100"
                    clearable
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="申报价格：">
                  <el-input
                    v-model.trim="infoModel.declare_price"
                    placeholder="请输入"
                    type="number"
                    maxlength="10"
                    show-word-limit
                    clearable
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="产品品牌：">
                  <el-input
                    v-model.trim="infoModel.brand"
                    placeholder="请输入"
                    show-word-limit
                    maxlength="100"
                    clearable
                  ></el-input>
                </el-form-item>
              </el-col>

              <el-col :span="24">
                <el-form-item label="原产国：">
                  <el-input
                    v-model.trim="infoModel.origin"
                    placeholder="请输入"
                    maxlength="100"
                    show-word-limit
                    clearable
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="采购成本：">
                  <el-input
                    v-model.trim="infoModel.purchase_cost"
                    type="number"
                    placeholder="请输入"
                    maxlength="10"
                    show-word-limit
                    clearable
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="产品单位：">
                  <el-input
                    v-model.trim="infoModel.company"
                    placeholder="请输入"
                    maxlength="150"
                    show-word-limit
                    clearable
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="描述：">
                  <el-input
                    type="textarea"
                    placeholder="请输入"
                    maxlength="500"
                    show-word-limit
                    v-model.trim="infoModel.customs_desc"
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-card>
        </div>

        <div class="foot mtop10 flexCenter">
          <el-button @click="resetForm">取消</el-button>
          <el-button type="primary" class="mleft50" :loading="loading" @click="submitForm">提交</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import UploadImgs from '@/component/base/upload-image'
import { vaildatorRules } from './rule'
import ProductApi from '@/api/product.js'

export default {
  name: 'addModal',
  components: {
    UploadImgs,
  },
  props: {
    infoModel: {
      type: Object,
      default: () => ({}),
    },
    showModel: {
      type: Boolean,
      default: () => false,
    },
  },

  watch: {
    showModel: {
      handler(val) {
        if (val) {
          console.log(this.infoModel.category_id, 'this.infoModel.category_id')
          if (!this.categoryList.length) {
            this.getProductTypeHandler()
          }

          if (!this.toExamineProductList.length) {
            this.getToExamineProductHandler()
          }
        }
      },
      deep: true,
      immediate: true,
    },
  },

  data() {
    return {
      rules: vaildatorRules(this),
      photoRules: {
        minWidth: 100,
        minHeight: 100,
        maxSize: 5,
      },
      shopsList: [],
      BarcodeNameList: [],
      categoryList: [],
      toExamineProductList: [],
      loading: false,
    }
  },

  methods: {
    // 获取已审核的独立产品
    async getToExamineProductHandler() {
      try {
        const RES = await ProductApi.getToExamineProduct()
        console.log(RES, '===============')
        this.toExamineProductList = RES.data || []
      } catch (error) {
        console.log(error)
      }
    },

    // 查找重复数据
    filterLevelChange(list, type) {
      const flagid = [] // 这数组不能有相同的id
      list.forEach(item => {
        if (item[type]) {
          flagid.push(item[type])
        }
      })

      // 判断数据是否有重复数据
      const isRepeat = arr => {
        const tatget = []
        const hash = {}
        for (const i in arr) {
          if (hash[arr[i]]) {
            tatget.push(arr[i])
          }
          hash[arr[i]] = true
        }
        return tatget
      }
      return isRepeat(flagid)
    },

    removeItem(i, type) {
      if (type == 'shop_code') {
        this.infoModel.shops.splice(i, 1)
      } else if (type == 'custom_code') {
        this.infoModel.product_barcodes.splice(i, 1)
      } else if (type == 'portfolio_products') {
        this.infoModel.portfolio_products.splice(i, 1)
      }
    },

    addHandler(type) {
      if (type === 'shop_code') {
        this.infoModel.shops.push({ shop_sku: '', shop_code: '' })
      } else if (type == 'custom_code') {
        this.infoModel.product_barcodes.push({ custom_code_name: '', custom_code: '' })
      } else if (type == 'portfolio_products') {
        this.infoModel.portfolio_products.push({ son_id: '', son_num: '' })
      }
    },

    /**
     * 产品品类
     */
    async getProductTypeHandler() {
      try {
        const RES = await ProductApi.getProductType()
        console.log(RES, '产品品类')
        this.categoryList = RES.data || []
      } catch (error) {
        console.log(error)
      }
    },

    submitForm() {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          try {
            this.loading = true

            let res = null
            const params = { ...this.infoModel }
            params.is_battery = Number(params.is_battery)
            params.is_liquid = Number(params.is_liquid)
            params.is_magnetized = Number(params.is_magnetized)

            if (params.category_id && params.category_id.length) {
              params.category_id = params.category_id[params.category_id.length - 1]
            } else {
              params.category_id = null
            }
            params.packag_type = 63
            const PhotoList = await this.$refs.photoEle.getValue()

            params.picture_url = (PhotoList.length && PhotoList[0].display) || ''
            // console.log(params, '请求参数======', PhotoList, PhotoList[0], PhotoList[0].display)
            if (params.isAdd == 'add') {
              res = await ProductApi.addProductHandler(params)
            } else {
              res = await ProductApi.editProductHandler(params)
            }
            this.loading = false

            if ((params.isAdd == 'add' && res.code == 1) || (params.isAdd == 'edit' && res.code == 2)) {
              this.$message({
                message: '提交成功',
                type: 'scuccess',
              })
              this.resetForm()
            } else {
              this.$message({
                message: `提交失败：${res.message}`,
                type: 'warning',
              })
            }
          } catch (error) {
            console.error(error)
            this.loading = false
            this.$message({
              message: `网络异常/提交失败：${error}`,
              type: 'warning',
            })
          }
        }
      })
    },
    resetForm() {
      this.$refs.ruleForm.resetFields()
      this.returnHandler()
    },
    returnHandler() {
      this.$emit('close', 'close')
    },
  },
}
</script>

<style lang="scss" scoped>
.infoBox {
  width: 100%;
  height: 100%;
}

.leftbox {
  border-radius: 8px;
  background-color: #e9eef3;
  border: 1px solid #d4cfcf;
  width: 100%;
  height: 100%;
  background-color: #e9eef3;
}

.rightbox {
  width: 560px;
  min-height: 500px;
  border-radius: 8px;
  background-color: #e9eef3;
  border: 1px solid #d4cfcf;
}

.photo {
  height: 160px;
  width: 160px;
  // border: 1px solid #d4cfcf;
  margin-left: 20px;
  border-radius: 8px;
}
.foot {
  width: 100%;
  border-radius: 8px;
  height: 70px;
  border: 1px solid #d4cfcf;
  background-color: #e9eef3;
}

.mr50 {
  margin-right: 50px;
}
</style>
